package android.wheel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.dspsemi.wodi.R;

public class CityDBHelper {
	private static CityDBHelper sDatabaseHelper;

	private SQLiteDatabase mSQLiteDatabase;

	private Context mContext;

	private CityDBHelper() {

	}

	public void prepare(Context c) {
		this.mContext = c;
		try {
			String path = c.getFilesDir().getParentFile().getPath()
					+ "/databases";
			File dir = new File(path);
			if (!dir.exists()) {
				dir.mkdir();
			}
			String db_path = path + "/cities.db";
			File dirDatabase = new File(db_path);
			if (dirDatabase.exists()) {
				Log.d("test", "db已存在，不用重复创建啦");
				return;
			}

			dirDatabase.createNewFile();
			InputStream is = c.getResources().openRawResource(R.raw.cities);
			FileOutputStream fos = null;
			fos = new FileOutputStream(dirDatabase);

			byte[] buffer = new byte[1024 * 4];
			int count = 0;
			while ((count = is.read(buffer)) > 0) {
				fos.write(buffer, 0, count);
			}
			fos.close();
			is.close();
		} catch (Exception e) {
			Log.d("test", "创建db异常 e = " + e.toString());
		}
	}

	public void close() {
		if (null != mSQLiteDatabase) {
			if (mSQLiteDatabase.isOpen()) {
				mSQLiteDatabase.close();
				mSQLiteDatabase = null;
			}
		}
	}

	// pid = 0 的是province , 返回list
	public ArrayList<CityItem> getAllProvices() {
		final Cursor c = mSQLiteDatabase.query(CityFields.TABLE_NAME, null,
				CityFields.PID + " = 0", null, null, null, CityFields.ID);
		final ArrayList<CityItem> list = new ArrayList<CityItem>();
		CityItem b = null;
		while (c.moveToNext()) {
			b = convertToCity(c);
			list.add(b);
		}
		Log.d("test", "getAllProvices size = " + list.size());
		return list;
	}

	// 返回的是数组
	public CityItem[] getAllProvicesArray() {
		final Cursor c = mSQLiteDatabase.query(CityFields.TABLE_NAME, null,
				CityFields.PID + " = 0", null, null, null, CityFields.ID);

		int size = c.getCount();
		CityItem[] ci = new CityItem[size];
		CityItem b = null;
		int i = 0;

		while (c.moveToNext()) {
			b = convertToCity(c);
			ci[i] = b;
			i++;
		}

		return ci;
	}

	// 返回的是list
	public ArrayList<CityItem> getCitesByProvinceID(int id) {
		final Cursor c = mSQLiteDatabase.query(CityFields.TABLE_NAME, null,
				CityFields.PID + " = " + id, null, null, null, CityFields.ID);
		final ArrayList<CityItem> list = new ArrayList<CityItem>();
		CityItem ci = null;
		while (c.moveToNext()) {
			ci = convertToCity(c);
			list.add(ci);
		}
		Log.d("test", "getCitesByProvinceID size = " + list.size());
		return list;
	}

	// 返回的是数组
	public CityItem[] getCitesArrayByProvinceID(int id) {
		final Cursor c = mSQLiteDatabase.query(CityFields.TABLE_NAME, null,
				CityFields.PID + " = " + id, null, null, null, CityFields.ID);

		int size = c.getCount();
		CityItem[] ci = new CityItem[size];

		CityItem b = null;
		int i = 0;
		while (c.moveToNext()) {
			b = convertToCity(c);
			ci[i] = b;
			i++;
		}
		return ci;
	}

	public String getCityNameByID(int id) {
		final Cursor c = mSQLiteDatabase.query(CityFields.TABLE_NAME, null,
				CityFields.ID + " = " + id, null, null, null, CityFields.ID);
		CityItem ci = null;
		while (c.moveToNext()) {
			ci = convertToCity(c);
		}

		return ci.getName();
	}

	private CityItem convertToCity(Cursor c) {
		CityItem ci = new CityItem();
		ci.setId(c.getInt(c.getColumnIndex(CityFields.ID)));
		ci.setName(c.getString(c.getColumnIndex(CityFields.NAME)));
		ci.setPid(c.getInt(c.getColumnIndex(CityFields.PID)));
		return ci;
	}

	public static CityDBHelper getInstance() {
		if (sDatabaseHelper == null) {
			sDatabaseHelper = new CityDBHelper();
		}
		return sDatabaseHelper;
	}

	public void open() {
		mSQLiteDatabase = SQLiteDatabase.openOrCreateDatabase(mContext
				.getFilesDir().getParentFile().getPath()
				+ "/databases/cities.db", null);
	}

	public CityItem getProviceByCityID(int cityId) {
		Cursor c = mSQLiteDatabase
				.query(CityFields.TABLE_NAME, null, CityFields.ID + " = "
						+ cityId, null, null, null, CityFields.ID);
		CityItem ci = null;
		while (c.moveToNext()) {
			ci = convertToCity(c);
		}

		int pid = ci.getPid();

		Cursor c2 = mSQLiteDatabase.query(CityFields.TABLE_NAME, null,
				CityFields.ID + " = " + pid, null, null, null, CityFields.ID);
		CityItem ci2 = null;
		while (c2.moveToNext()) {
			ci2 = convertToCity(c2);
		}

		return ci2;
	}
}
